package com.mark.mall.web.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import com.mark.mall.web.platform.dto.CartListDTO;
import com.mark.mall.web.platform.dto.param.CartAddParam;
import com.mark.mall.web.platform.entity.Cart;
import com.mark.mall.web.platform.entity.Product;
import com.mark.mall.web.platform.mapper.CartMapper;
import com.mark.mall.web.platform.mapper.ProductMapper;
import com.mark.mall.web.platform.service.CartService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-03-03
 */
@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    private final ProductMapper productMapper;

    @Override
    public R<Boolean> addCart(List<CartAddParam> list) {
        if (list == null || list.size() == 0) {
            throw new MyException("参数缺失，请核实！");
        }
        for (CartAddParam cartAddParam : list) {
            if (cartAddParam.getNum() <= 0) {
                continue;
            }
            Cart cart = new Cart();
            BeanUtils.copyProperties(cartAddParam, cart);
            cart.setCreatetime(LocalDateTime.now());
            super.save(cart);
            //cartMapper.insert(cart);
        }
        return new R<>(true, "购物车新增成功！");
    }


    @Override
    public R<List<CartListDTO>> cartList(String memberId) {
        if (StringUtils.isEmpty(memberId)) {
            throw new MyException("参数缺失，请核实！");
        }
        // 根据会员id获取该会员所有购物车数据
        List<Cart> list = super.lambdaQuery()
                .eq(Cart::getMemberId, memberId)
                .list();
        List<CartListDTO> dtoList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Cart cart : list) {
                if (cart == null) continue;
                Product product = productMapper.selectById(cart.getProductId());
                dtoList.add(new CartListDTO(product, cart));
            }
        }

        return new R<>(dtoList);
    }

    @Override
    public R<Boolean> delCart(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new MyException("参数缺失，请核实！");
        }
        // 购物车直接删除
        baseMapper.deleteById(id);
        return new R<>(true, "删除成功！");
    }

    @Override
    public R<Boolean> updateCartNum(String id, int num) {
        if (num < 0) {
            throw new MyException("数量不能再小了！");
        }
        Cart cart = baseMapper.selectById(id);
        //Cart cart = super.getOne(super.lambdaQuery().eq(Cart::getId, id));
        if (cart == null) {
            throw new MyException("id为{" + id + "}的购物车不存在！");
        }
        //cart.setNum(num);
        //super.update(cart, new QueryWrapper<Cart>().lambda().eq(Cart::getId, id));
        //baseMapper.update(cart, new QueryWrapper<Cart>().lambda().eq(Cart::getId, id));
        update(Wrappers.<Cart>lambdaUpdate().set(Cart::getNum, num)
                .eq(Cart::getId, id));
        return new R<>(true, "修改成功！");
    }
}
